# 数据操作类
from MySQl import MySQlUtil
import ast
class Data:
    def __init__(self):
        self.Feature = [] #所有特征
        self.KB = [] #所有知识
        self.Animal = []  #所有动物名
        self.mySQlUtil = MySQlUtil()

    def dataBase(self):
        # 从数据库得到所有特征并转换为列表
        tp_features = self.mySQlUtil.selectAllFeature()
        for tp_feature in tp_features:
            self.Feature.append(tp_feature[0])
        #从数据库获得所有的动物
        tp_animals = self.mySQlUtil.selectAllAnimal()
        for tp_animal in tp_animals:
            self.Animal.append(tp_animal[0])

        #从数据库获得所有知识
        tp_rules = self.mySQlUtil.selectAllRule()
        # print("tp_rules",tp_rules)
        for tp_rule in tp_rules:
            li_rule_1 = [] #存储一条规则,带id
            pres = ast.literal_eval(tp_rule[1]) #前提
            li_rule_1.append(tp_rule[0]) #加入id
            pres = ast.literal_eval(tp_rule[1])
            # print("pres", pres)
            for pre in pres:#加入前提
                li_rule_1.append(pre)
            li_rule_1.append(tp_rule[-1]) #加入结论
            # print("tp_rule[0]",tp_rule[0])
            # print("tp_rule[-1]", tp_rule[-1])
            self.KB.append(li_rule_1)